package Controller1;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import Dao.goodsDao;
import Model1.goods;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseDragEvent;
import javafx.stage.Stage;

public class GoodsController {
	@FXML
	private TableView<goods> table = new TableView<>();
	@FXML
	private TextField goods_id;
	@FXML
	private TextField goods_name;
	@FXML
	private TextField goods_kind;
	@FXML
	private TextField goods_inventory;
	@FXML
	private TextField goods_sellingPrice;
	@FXML
	private TextField goods_costPrice;
	
	@FXML
	private TableColumn Goods_id = new TableColumn("ID");
	@FXML
	private TableColumn Goods_name = new TableColumn("名称");
	@FXML
	private TableColumn Goods_kind = new TableColumn("类型");
	@FXML
	private TableColumn Goods_inventory = new TableColumn("数量");
	@FXML
	private TableColumn Goods_costPrice = new TableColumn("进价");
	@FXML
	private TableColumn Goods_sellingPrice = new TableColumn("售价");
	
	public void initialize() {
		//Style.getItems().addAll("奶茶类", "果汁类", "咖啡类", "冰淇淋类");
		//Style.getSelectionModel().select("奶茶类");
		//sx(ProductionDao.findAll());
		//refresh(goodsDao.findAll());
		b();
		table.addEventFilter(MouseDragEvent.MOUSE_CLICKED, event -> {
			if (table.getSelectionModel().getSelectedItem()!= null) {
				goods good = table.getSelectionModel().getSelectedItem();
				//System.out.print(good.getKind());
				goods_id.setText(good.getId()+"");
				goods_name.setText(good.getName());
				goods_kind.setText(good.getKind()+"");
				goods_inventory.setText(good.getInventory()+"");
				goods_sellingPrice.setText(good.getSellingPrice()+"");
				goods_costPrice.setText(good.getCostPrice()+"");
				//Style.getSelectionModel().select(production.getCategory());
				//this.showPhoto(production);
			}
		});

	}
	public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("View1/Table_Inventory.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.setResizable(false);
        primaryStage.show();
    }
	
	public void refresh(List<goods> goods_Table) {
		ObservableList<goods> data = FXCollections.observableArrayList(goods_Table);
		Goods_id.setCellValueFactory(new PropertyValueFactory<>("id"));
		Goods_name.setCellValueFactory(new PropertyValueFactory<>("name"));
		Goods_kind.setCellValueFactory(new PropertyValueFactory<>("kind"));
		Goods_inventory.setCellValueFactory(new PropertyValueFactory<>("inventory"));
		Goods_sellingPrice.setCellValueFactory(new PropertyValueFactory<>("sellingPrice"));
		Goods_costPrice.setCellValueFactory(new PropertyValueFactory<>("costPrice"));
		//goods_id.setText("2");
		//goods_kind.setText("");
		//goods_inventory.setText("");
		//goods_sellingPrice.setText("");
		//goods_costPrice.setText("");
		//image.setImage(null);
		table.setItems(data);
		table.getSelectionModel().clearSelection();
		//image.setImage(null);
	}
	
	public void insert() {//增
		try {
			goods good=new goods(goods_name.getText(), Integer.parseInt(goods_id.getText()),Float.parseFloat(goods_sellingPrice.getText()),Float.parseFloat(goods_costPrice.getText()),Integer.parseInt(goods_inventory.getText()),Integer.parseInt(goods_kind.getText()));
			Boolean a=goodsDao.Insert(good);
		}catch(Exception e) {
			JOptionPane.showMessageDialog(null, "插入数据错误，请重试"); 
			e.printStackTrace();
		}
		
		b();
	}
	
	
	public void delete() {
		try {
			int id=Integer.parseInt(goods_id.getText());
			Boolean a=goodsDao.Delete(id);	
		}catch(Exception e) {
			JOptionPane.showMessageDialog(null, "删除数据错误，请重试"); 
			e.printStackTrace();
		}
		b();
	}
	
	public void b() {
		refresh(goodsDao.findAll());
	}
	
	public void search() {
		try {
			int id=Integer.parseInt(goods_id.getText());
			goods good1=goodsDao.SearchById(id);
			List<goods> good_group=new ArrayList<>();
			good_group.add(good1);
			refresh(good_group);
		}catch(Exception e) {
			JOptionPane.showMessageDialog(null, "查找数据错误，请重试"); 
			e.printStackTrace();
		}
	}
	
	public void Update() {
		try {
			
			goods good=new goods(goods_name.getText(), Integer.parseInt(goods_id.getText()),Float.parseFloat(goods_sellingPrice.getText()),Float.parseFloat(goods_costPrice.getText()),Integer.parseInt(goods_inventory.getText()),Integer.parseInt(goods_kind.getText()));
			Boolean a=goodsDao.Update(good);
		}catch(Exception e) {
			JOptionPane.showMessageDialog(null, "修改数据错误，请重试"); 
			e.printStackTrace();
		}
		b();
	}
	public void back() {
		Stage stage = (Stage)goods_id.getScene().getWindow();
		stage.close();
		MainViewController mc = new MainViewController();
		try {
			mc.start(stage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}